package com.jtxy.npms.controller;

import com.jtxy.npms.entity.AQI;
import com.jtxy.npms.entity.AQIFeedback;
import com.jtxy.npms.utils.FileUtils;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;

import java.util.Map;

public class SupervisorQueryAQIViewController {
    @FXML
    private TableView<AQIFeedback> tableView;

    @FXML
    private TableColumn<AQI, String> afIdColumn, telIdColumn,levelColumn, provinceColumn, cityColumn,addressColumn,feedbackColumn,dateColumn;
    private Map<String, AQIFeedback> AqiFeedbackMap;
    @FXML
    //        设置数据源，观察者模式来监听数据源的变化
    private ObservableList<AQIFeedback> aqiFeedbackList = FXCollections.observableArrayList();

    public void initialize()
    {
        tableView.setItems(aqiFeedbackList);
        afIdColumn.setCellValueFactory(new PropertyValueFactory<>("afId"));
        telIdColumn.setCellValueFactory(new PropertyValueFactory<>("telId"));
        levelColumn.setCellValueFactory(new PropertyValueFactory<>("level"));
        provinceColumn.setCellValueFactory(new PropertyValueFactory<>("province"));
        cityColumn.setCellValueFactory(new PropertyValueFactory<>("city"));
        addressColumn.setCellValueFactory(new PropertyValueFactory<>("address"));
        feedbackColumn.setCellValueFactory(new PropertyValueFactory<>("feedback"));
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("date"));

         AqiFeedbackMap = FileUtils.readObject("aqiFeedback.txt");
//        读取map中的每个数据，添加进aqiList
        aqiFeedbackList.addAll(AqiFeedbackMap.get(telIdColumn));
        tableView.setEditable(true);
        addressColumn.setCellValueFactory(TextFieldTableCell.forTableColumn());
        addressColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AQI, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<AQI, String> event) {
                AQIFeedback aqiFeedback = event.getRowValue();
                String newAddress = event.getNewValue();
                aqiFeedback.setAddress(newAddress);
                AqiFeedbackMap.get(telIdColumn).
            }
        });


    }

    public void delAqiFeedback(){
        tableView.getSelectionModel().getSelectedItem();
        //将这个对象从List中删除，然后还需要将对象从Map中删除，然后重新写入文件
        aqiFeedbackList.remove(AQIFeedback);

    }
}
